package com.controller;

import com.bean.Customer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
public class JsonController {

    @RequestMapping("testJson")
    public String JsonMethod() {
        Customer cu=new Customer();
        cu.setId(11);
        cu.setUname("admin5");
        cu.setPassword("5555");
        cu.setEmail("111@163.com");
        cu.setAge(19);

        Customer cu1=new Customer();
        cu1.setId(22);
        cu1.setUname("22");
        cu1.setPassword("22");
        cu1.setEmail("111@22.com");
        cu1.setAge(22);

        Customer cu2=new Customer();
        cu2.setId(33);
        cu2.setUname("33");
        cu2.setPassword("33");
        cu2.setEmail("111@3.com");
        cu2.setAge(3);

        List<Customer> list = new ArrayList<>();
        list.add(cu);
        list.add(cu1);
        list.add(cu2);

        ObjectMapper obj = new ObjectMapper();

        try {
            //返回对象结构-json数据
//            return obj.writeValueAsString(cu);
            return obj.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;


    }
}
